<!-- desc: 内部领用单_审核-->

<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="dynamicApiConfigs"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-fix-choose="false"
      :dynamic-is-fix-index="false"
      :callback-obj="callbackObj"
      :dynamic-is-show-more-btn="true"
      :dynamic-api-style="'restful'"
    />
    <detailList
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-state="editPopupsState"
      :popups-visible.sync="editPopupsVisible"
      :dynamic-api-style="'restful'"
      @close="close"
    />
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      title="审核意见"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      width="50%"
      @close="closed"
    >
      <dialogHeader
        slot="title"
        title="审核意见"
        :style-type="$store.state.app.dialogType"
      />
      <div class="filter-container filter-params">
        <el-row ref="vailComponentList" :gutter="24">
          <el-col :span="3"><span>审核意见:</span></el-col>
          <el-col :span="21">
            <el-input
              v-model="textarea"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
            />
          </el-col>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons1"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
    <!-- <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible1="editPopupsVisible1"
      :popups-state="editPopupsState"
      :dynamic-api-style="'restful'"
      @close="close"
    /> -->
  </div>
</template>

<script>
import { formMixins } from '@/components/mixins/formMixins'
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/vehicleManagement'
import { orgApis } from '@/api/paSparePartsManagement'

export default {
  name: 'SalesSheetMaking',
  components: {
    OneTableTemplate,
    dialogHeader: () => import('@/components/dialogHeader'),
    DetailList: () => import('./detailList')
    // Edit: () => import('./edit')
  },
  mixins: [oneTableWithViewTemplateMixins, formMixins],
  data() {
    return {
      editRowData: {},
      selectiondata: [],
      aa: false,
      curPopupsVisible: false,
      textarea: '',
      dynamicApiConfigs: orgApis.RinternalRequisitionCheckQuery,
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              width: 100,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '查看明细', // 编辑
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: (index) => { this.edit(index) },
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'requisitionCode',
              label: '领用单号',
              align: 'center'
            },
            {
              prop: 'warehouseName',
              label: '领用仓库',
              align: 'center'
            },
            {
              prop: 'deptName',
              label: '领用部门',
              align: 'center'
            },
            {
              prop: 'budgetNumber',
              label: '预算号',
              align: 'center'
            },
            {
              prop: 'empName',
              label: '领用人',
              align: 'center'
            },
            {
              prop: 'requisitionVariety',
              label: '品种数',
              align: 'center'
            },
            {
              prop: 'requisitionUse',
              label: '领用用途',
              align: 'center'
            },
            {
              prop: 'receiver',
              label: '收货人',
              align: 'center'
            },
            {
              prop: 'receiverPhone',
              label: '联系电话',
              align: 'center'
            },
            {
              prop: 'receiverAddress',
              label: '收货地址',
              align: 'center'
            }
          ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 库存状态
            {
              compKey: 'compKey1',
              labelName: '领用单号：',
              codeField: 'requisitionCode',
              component: () => import('@/components/org/commonInput'),
              oFields: 'AsId,AsText',
              type: 'inputText',
              isMust: true,
              span: 6
            },
            // 车辆状态
            {
              compKey: 'compKey2',
              labelName: '领用仓库：',
              codeField: 'warehouseId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              customObj: {
                api: orgApis.queryCK,
                defOptionFields: ['warehouseId', 'warehouseName']
              },
              isRequire: false,
              isMust: true,
              span: 6
            },
            // 入库仓库
            {
              compKey: 'compKey3',
              labelName: '领用部门：',
              codeField: 'deptName',
              component: () => import('@/components/org/commonInput'),
              isMust: true,
              span: 6
            },
            // 审核状态
            {
              compKey: 'compKey4',
              labelName: '领用人：',
              codeField: 'empName',
              component: () => import('@/components/org/commonInput'),
              isMust: true,
              span: 6
            },
            {
              compKey: 'compKey5',
              labelName: '收货人：',
              codeField: 'receiver',
              component: () => import('@/components/org/commonInput'),
              isMust: false,
              span: 6
            },
            {
              compKey: 'compKey6',
              labelName: '联系电话：',
              codeField: 'receiverPhone',
              component: () => import('@/components/org/commonInput'),
              isMust: false,
              span: 6
            }
          ],
      tableButtons: [
        {
          conmpKey: 'btenKey1',
          type: 'primary',
          size: 'small',
          text: '查询',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          position: 'right'
        },
        {
          conmpKey: 'btenKey2',
          type: '',
          size: 'small',
          text: '审核',
          name: '',
          clickEvent: () => this.getSave(),
          position: 'right'
        },
        {
          conmpKey: 'btenKey3',
          type: '',
          size: 'small',
          text: '驳回',
          name: '',
          clickEvent: () => this.close(),
          position: 'right'
        }
      ],
      tableButtons1: [
        {
          conmpKey: 'btenKey4',
          type: 'primary',
          size: 'small',
          text: '保存',
          name: 'search',
          clickEvent: () => this.getBack(),
          position: 'right'
        },
        {
          conmpKey: 'btenKey5',
          type: '',
          size: 'small',
          text: '取消',
          name: '',
          clickEvent: () => this.closed(),
          position: 'right'
        }
      ],
      formField: this.$utils.getFormField(this, {
        requisitionCode: '',
        warehouseId: '',
        deptName: '',
        empName: '',
        receiver: '',
        receiverPhone: ''
      }),
      callbackObj: {
        // 手动勾选网格时，拿到勾选网格的数据
        onCheckSelectChange: (selection, row) => {
          this.selectiondata = selection
        }
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  methods: {
    edit(index) {
      this.editPopupsState = 'edit'
      this.editPopupsVisible = true
      this.editRowData = this.$utils.deepCopy(this.$refs['multipleTable'].list[index])
      //
    },
    open() {
      this.curPopupsVisible = true
    },
    closed() {
      this.curPopupsVisible = false
      this.textarea = ''
    },
    close() {
      console.log(this.selectiondata, '222')
      if (this.selectiondata == undefined || this.selectiondata.length == '0') {
        this.$message({
          message: '请先勾选要驳回的数据',
          type: 'success',
          duration: 2000
        })
      } else {
        this.curPopupsVisible = true
      }
    },
    getSave() {
      const that = this
      const infoQue = { examineList: [] }
      if (this.selectiondata == undefined || this.selectiondata.length == '0') {
        this.$message('请先勾选要审核的数据!')
      } else {
        this.selectiondata.forEach(function(val, index) {
          infoQue.examineList.push({
            requisitionId: val.requisitionId,
            updateControlId: val.updateControlId
          })
        })
        const queryObj = {
          apiConfig: orgApis.internalRequis,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: {
            // 当前中台使用的名称有dataInfo、info，具体查看API文档
            dataInfo: infoQue
          }
        }
        // 转换了中台请求格式数据
        var paramD = that.$getParams(queryObj)
        // 调用中台API方法（可复用）
        that.$requestAPI(paramD).then(response => {
          if (response.result === '0') {
            return this.$message(response.msg || '审核失败！')
          }
          var getResult = response
          if (getResult.result === '1') {
            that.$message(response.msg || '审核成功！')
            // 重置表单
            this.queryTable(1)
            this.selectiondata = []
            // 重新加载
            // that.onLeftRowClick(null);
          }
        })
      }

      // }
    },
    getBack() {
      const that = this
      const infoQueList = { examineList: [], rejectReason: '' }
      this.selectiondata.forEach(function(val, index) {
        infoQueList.examineList.push({
          requisitionId: val.requisitionId
        })
      })
      if (this.textarea == '') {
        that.$message({
          message: '审核意见不能为空',
          type: 'warning',
          duration: 2000
        })
      } else {
        infoQueList.rejectReason = this.textarea
      }
      const queryObj = {
        apiConfig: orgApis.internalRequisBack,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: infoQueList
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        if (response.result === '0') {
          return
        }
        var getResult = response
        if (getResult.result === '1') {
          that.$message({
            message: '驳回成功',
            type: 'success',
            duration: 2000
          })
          this.curPopupsVisible = false
          that.queryTable(1)
          // 重置表单
          // that.resetRight(tableRef)
          // 重新加载
          that.onLeftRowClick(null)
        } else {
          that.$message({
            message: getResult.msg,
            type: 'warning',
            duration: 2000
          })
        }
      })
      // }
    }
  }
}
</script>

<style>
</style>
